package com.cloud.basic.copyToSharefile;

import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
 
public class SharedFolderAccess {
 
    public static void main(String[] args) {
        // 定义共享文件夹路径和待复制文件路径
        String sharedFolderPath = "\\\\192.168.3.5\\文件夹\\TestUploadFile\\";
        String sourceFilePath = "D:\\websocket\\websocket-server.jar";
 
        // 创建Path对象
        Path source = Paths.get(sourceFilePath);
        Path destination = Paths.get(sharedFolderPath + "websocket-server.jar");
 
        // 复制共享文件夹到对应部署的文件夹中
        try {
            Files.deleteIfExists(source);
            Files.copy(destination,source);
            System.out.println("文件复制成功！");
 
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
 
    // 自动输入字符串
    private static void autoType(String text) {
        Robot robot = null;
        try {
            robot = new Robot();
            robot.setAutoDelay(20);
            for (char c : text.toCharArray()) {
                robot.keyPress(KeyEvent.VK_ALT);
                robot.keyPress(KeyEvent.VK_CONTROL);
                robot.keyPress(KeyEvent.VK_DELETE);
                robot.keyRelease(KeyEvent.VK_DELETE);
                robot.keyRelease(KeyEvent.VK_CONTROL);
                robot.keyRelease(KeyEvent.VK_ALT);
                robot.delay(20);
 
                if (c == '\n') {
                    robot.keyPress(KeyEvent.VK_ENTER);
                    robot.keyRelease(KeyEvent.VK_ENTER);
                } else {
                    robot.keyPress(c);
                    robot.keyRelease(c);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (robot != null) {
                try {
                    Thread.sleep(1000); // 等待1秒，以便用户可以看到输入的内容
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                robot.keyPress(KeyEvent.VK_ENTER);
                robot.keyRelease(KeyEvent.VK_ENTER);
            }
        }
    }
}